import FColliderManager from "./FColliderManager";

const { ccclass, property } = cc._decorator;
export enum ColliderType {
    Circle,
    Box,
    Polygon,//凸多边形
}

export enum StateType {
    IsTest = 1,
    NoTest,
}

export interface ContactItem {
    other: FCollider,
    state: StateType,//1 已经碰撞检测 2未进行碰撞检测后续直接回调离开
}

@ccclass
export default class FCollider extends cc.Component {
    public get type() {
        return ColliderType.Box;
    }
    public isConvex: boolean = true;
    //自增id
    private static _baseId: number = 1;

    //AABB
    public aabb: cc.Rect = cc.rect();

    public get x() { return this.aabb.x; }
    public get y() { return this.aabb.y; }
    public get width() { return this.aabb.width; }
    public get height() { return this.aabb.height; }

    public colliderId: number = 0;
    public contactMap: Map<number, ContactItem> = new Map();
    public initCollider() {
        this.colliderId = FCollider._baseId++;
        if (FCollider._baseId > 5e10) {//防止id太大，做个轮回
            FCollider._baseId = 1;
        }
    }

    @property(cc.Vec2)
    private _offset: cc.Vec2 = cc.v2();

    @property(cc.Vec2)
    public get offset(): cc.Vec2 {
        return this._offset;
    }
    public set offset(value: cc.Vec2) {
        this._offset = value;

    }

    protected onEnable(): void {
        FColliderManager.instance.addCollider(this);
    }

    protected onDisable(): void {
        FColliderManager.instance.removeCollider(this);
    }
}